<?php
class ABDir{
	private $s_path = '';
	private $s_regexp = '';
	function __construct($s_path ='', $s_regexp = ''){
		$this->s_path = $s_path;
		$this->s_regexp = $s_regexp;
	}

	function setDir($s_dir){
		if(is_dir($s_dir)){
			$this->s_path = $s_dir;
		}else{
			throw new ABException('Not is a dir');
		}
	}

	function ls(){
		$o_dir=dir($this->s_path);
		$a_temp = array();
		while ($s_file = $o_dir->read()){
			$o_file = new stdClass();
			if(($s_file!='.')&&($s_file!='..')){
				if(!empty($this->regexp) and !preg_match($this->regexp, $s_file))continue;
				$o_file->name  = $s_file;
				$o_file->date  = date('d/m/Y H:i:s', filemtime($o_dir->path.'/'.$s_file));
				$o_file->size  = ABDir::getfilesize(filesize($o_dir->path.'/'.$s_file));
				$o_file->isdir = is_dir($o_dir->path.'/'.$s_file);
				$a_temp[] = $o_file;
			}
		}
		$o_dir->close();
		return $a_temp;
	}
	
	function lsdir(){
		return array_filter($this->ls(), array($this, 'dir_filter'));
	}

	private function dir_filter($a_array){
		return $a_array['isdir'];
	}
	
	static function getfilesize($i_size) {
		$a_unit = array(' bytes', ' KB', ' MB', ' GB', ' TB');
		$i = 0;
		while($i_size > 1024) { $i_size /= 1024; $i++;}
		return round($i_size, 2).$a_unit[$i];
	}

	function upload($s_name, $a_allowext,$_MSIZE = 50000){
		$o_fupload = $_FILES[$s_name];
		if(!$o_fupload)return 512;
		if($o_fupload['error'] != 0) return $o_fupload['error'];
		if (is_uploaded_file($o_fupload['tmp_name'])) {
			if($o_fupload['size'] > $_MSIZE) return 32;
			$s_ext=substr($o_fupload['name'] ,strrpos($o_fupload['name'], '.')+1);
			if (!in_array(strtolower($s_ext), $a_allowext))return 64;
			if (!copy($o_fupload['tmp_name'], $this->s_path.'/'.$o_fupload['name']))return 128;
			return 0;
		}
		return -1;
	}

	function download($s_file){
		$s_filepath = $this->s_path . $s_file;
		if (!is_file($s_filepath)) { return false; }
		header('Content-Type: application/octet-stream');
		header('Content-Size: '.filesize($s_filepath));
		header('Content-Disposition: attachment; filename="'.basename($s_filepath).'"');
		header('Content-Length: ' .filesize($s_filepath));
		header('Content-transfer-encoding: binary');
		readfile($s_filepath);
		exit;
	}

	function errorString($num){
	$value = array(
		UPLOAD_ERR_INI_SIZE => 'El archivo superó el tamaño establecido por la directiva <em>upload_max_filesize</em> de php.ini',
		UPLOAD_ERR_FORM_SIZE => 'El archivo superó el tamaño establecido por la directiva <em>MAX_FILE_SIZE</em>',
		UPLOAD_ERR_PARTIAL => 'El archivo no subió completamente',
		UPLOAD_ERR_NO_FILE => 'El archivo no subió.', 
		UPLOAD_ERR_NO_TMP_DIR =>'Perdido el directorio temporal.',
		UPLOAD_ERR_CANT_WRITE => 'No se puede escribir el archivo en el disco',
		UPLOAD_ERR_EXTENSION  => 'Carga del archivo detenida por la extensión',
		32 =>'El archivo es muy grande', 64 => 'El tipo archivo tipo no está permitido.',
		128 => 'No se pudo copiar el archivo', 512 => 'Archivo no valido');
		return $value[$num];
	}

	function read($s_file){
		$s_filepath = $this->s_path . $s_file;
		return file_get_contents($s_filepath);
	}

	function delete($s_file){
		$s_filepath = $this->s_path . $s_file;
		if (!is_file($s_filepath))return false;
		return unlink($s_filepath);
	}
}
?>
